window.pageNo = $("#pageNo").val()||1;
window.pageCount = $("pageCount").val()||10;
var app = angular.module('AlarmApp', []);
//app.controller('alarmAddCtrl', function($scope, $http) {
//	/*$http.post("addAlarm.json").success(function(response) {
//		$scope.response = response;
//	});*/
//});
app.controller('alarmListCtrl',function($scope, $http) {
	$scope.trClick = function(obj,$t){
		$scope.alarmId = obj.target.dataset.id;
		$("#example tr").removeClass("success");
		$(obj.target).parent().addClass("success");
		$.ajax({
			url:"loadAlarmById.json",
			type:"post",
			data:{id:$scope.alarmId},
			dataType:"json",
			success:function(res){
				$scope.alarm = res.data;
				$scope.$apply();
			}
		});
	}
	var httpUrl = "loadAlarmListBySearch.json";
	if($("#alarmTypeInput").length){
		httpUrl = httpUrl+"?type="+$("#alarmTypeInput").val();
	}
	$http.post(httpUrl)
		.success(function(response) {
					$scope.response = response;
					$scope.data = response.data;
					$scope.dataList = response.data.dataList;
				});
	$scope.searchBtn = function() {
		//alert("asd");
		$.ajax({url:"loadAlarmListBySearch.json",
			data:$("#searchParam").serialize(),
			dataType:'json',
			type:'post',
			success:function(response) {
				$scope.response = response;
				$scope.data = response.data;
				$scope.dataList = response.data.dataList;
			}
		});
	}
	$http.post("loadDataByDays.json").success(function(resp){
		var array = resp.arrays;
		var dataList = resp.dataList;
		$('#lineChart').highcharts(
				{
					title : {
						text : '警情总览',
						x : -20
					//center
					},
					subtitle : {
						text : '本月警情总览',
						x : -20
					},
					xAxis : {
						categories : [ ]
					},
					yAxis : {
						title : {
							text : '每天警情数'
						},
						plotLines : [ {
							value : 0,
							width : 1,
							color : '#808080'
						} ]
					},
					tooltip : {
						valueSuffix : '°C'
					},
					legend : {
						layout : 'vertical',
						align : 'right',
						verticalAlign : 'middle',
						borderWidth : 0
					},
					series : [
							{
								name : '火灾预警',
								data :	array[0]
							},
							{
								name : '设备预警',
								data : array[1]
							},
							{
								name : '电器预警',
								data : array[2]
							}]
				});
		$('#pineChart').highcharts({
	        chart: {
	            type: 'pie',
	            options3d: {
	                enabled: true,
	                alpha: 45,
	                beta: 0
	            }
	        },
	        title: {
	            text: '报警类型比例'
	        },
	        tooltip: {
	            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                depth: 35,
	                dataLabels: {
	                    enabled: true,
	                    format: '{point.name}'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '警情比例图',
	            data: dataList
	        }]
	    });
	});
});
app.controller('alarmDataCtrl', function($scope, $http) {
	$http.post("loadAlarmByType.json").success(function(resp){
		var dataList = resp.dataList, map = {
			0 : {
				name : '总报警事件',
				count : 0,
				btnClass:"btn btn-default"
			},
			1 : {
				name : '火警',
				count : 0,
				btnClass:"btn btn-primary"
			},
			2 : {
				name : '设备',
				count : 0,
				btnClass:"btn btn-success"
			},
			3 : {
				name : '电气',
				count : 0,
				btnClass:"btn btn-warning"
			}
		},
		total = 0;
		for(var key in dataList){
			var data = dataList[key];
			if(!data||!map[data.type]){
				return;
			}
			map[data.type].count = data.count;
			total+=+data.count;
			map[data.type].name = data.typeName;
		}
		map[0].count = total;
		$scope.alarmMap = map;
	});
});

app.controller('alarmAddCtrl', function($scope, $http) {
	$http.post("loadAllGrid.json").success(function(resp){
		$scope.allGrids= resp.dataList;
	});
	$scope.addAlarm = function(){
		$.ajax({
			url:'addAlarm.json',
			data:$("#addAlarmForm").serialize(),
			type:'post',
			dataType:'json',
			success:function(resp){
				if(resp.success){
					alert('添加成功');
					location.href='index.do';
				}
			}
		});
	}
});
app.controller('alarmDelCtrl', function($scope, $http) {
	$http.post("deleteAlarmById.json").success(function(response) {
		$scope.response = response;
	});
});
var gridInfoApp = angular.module("gridInfoApp",[]);
gridInfoApp.controller("newInfoCtrl",function($scope,$http){
	$http.post("loadNewById.json?id="+$("#newIdInput").val()).success(function(resp){
		$scope.data = resp.data;
	});
	$http.post("loadAllGrid.json").success(function(resp){
		$scope.data = resp.dataList;
	});
})
gridInfoApp.controller('gridAlarmCtrl', function($scope, $http) {
	$http.post("loadAlarmByType.json").success(function(resp){
		var dataList = resp.dataList, map = {
			0 : {
				name : '总报警事件',
				count : 0,
				btnClass:"btn btn-default"
			},
			1 : {
				name : '火警',
				count : 0,
				btnClass:"btn btn-primary"
			},
			2 : {
				name : '设备',
				count : 0,
				btnClass:"btn btn-success"
			},
			3 : {
				name : '电气',
				count : 0,
				btnClass:"btn btn-warning"
			}
		},
		total = 0;
		for(var key in dataList){
			var data = dataList[key];
			map[data.type].count = data.count;
			total+=+data.count;
			map[data.type].name = data.typeName;
		}
		map[0].count = total;
		$scope.alarmMap = map;
	});
});
gridInfoApp.controller("gridInfoCtrl",function($scope,$http){
	$http.post("loadAllGrid.json").success(function(resp){
		$scope.allGrids= resp.dataList;
	});
	$scope.chooseGrid = function(obj){
		var id = obj.data.id;
		$http.post("loadGridPerson.json?id="+id).success(function(resp){
			$scope.firePersons = resp.dataList;
			$scope.dutyPersons = resp.dataList1;
			$scope.grid = resp.data;
		});
	}
})
gridInfoApp.controller("gridNewsCtrl",function($scope,$http){
	$http.post("loadAllNews.json").success(function(resp){
		$scope.response = resp;
		for(var key in resp.map){
			var title = "网格化管理要闻";
			if(key==2){
				title="网格消防相关条例法规文件";
			}
			if(key==3){
				title="网格管理相关政府机构";
			}
			if(key==4){
				title="优秀网格";
			}
			if(key==5){
				title="优秀网格员";
			}
			resp.map[key].name = title;
		}
		$scope.map = resp.map;
	});
	$http.post().success(function(){});
});


var pollingApp = angular.module("PollingApp",[]);
pollingApp.controller("pollingCtrl",function($scope,$http){
	$.ajax({
		url:"loadPollingList.json",
		data:{},
		dataType:'json',
		success:function(resp){
			$scope.data = resp.data;
			$scope.dataList = resp.data.dataList;
			$scope.personList = resp.dataList;
			$scope.$apply();
		}
	});
	$scope.trClick = function(obj,$t){
		$scope.pollingId = obj.target.dataset.id;
		$("#example tr").removeClass("success");
		$(obj.target).parent().addClass("success");
		$.ajax({
			url:"loadPollingById.json",
			type:"post",
			data:{id:$scope.pollingId},
			dataType:"json",
			success:function(res){
				$scope.polling = res.data;
				$scope.$apply();
			}
		});
	}
	$scope.searchBtn = function(){
		$.ajax({
			url:"loadPollingList.json",
			data:$("#searchParam").serialize(),
			dataType:'json',
			type:'post',
			success:function(resp){
				$scope = resp;
			}
		});
	}
});

var userApp = angular.module("userApp",[]);
userApp.controller("userCtrl",function($scope){
	$.ajax({
		url:'loadUserBySearch.json',
		type:'post',
		data:{},
		dataType:'json',
		success:function(resp){
			$scope.userList = resp.dataList;
			$scope.$apply();
		}
	});
	$scope.trClick = function(obj,$t){
		$scope.userId = obj.target.dataset.id;
		$("#example tr").removeClass("success");
		$(obj.target).parent().addClass("success");
		$.ajax({
			url:"loadUserById.json",
			type:"post",
			data:{id:$scope.userId},
			dataType:"json",
			success:function(res){
				$scope.user = res.data;
				$scope.$apply();
			}
		});
	}
});
userApp.controller("userInfoCtrl",function($scope){
	$.ajax({
		url:'loadUserById.json',
		type:'post',
		data:{id:$("#userId").val()},
		dataType:'json',
		success:function(resp){
			$scope.user = resp.data;
			$scope.$apply();
		}
	});
});